//	--------------------------------------------------------------------
//	Copyright(C) 2006,2007 Zhao Yukun. All rights reserved.
//	This file is a part of the Xeres system.
//	Xeres 2007
//	Contact : xeres.engine@gmail.com
//	$(reservedInfo)
//	$(reservedDeclaration)
//	--------------------------------------------------------------------
///	@file	<CRenderer10.h>
///	@path	~/src/graphics/dx10/
///	@date	2008/12/20
///	@desc	.

#pragma once

#include "xeres.h"

#include "lib/utilities/dict.h"
#include "lib/utilities/time.h"

#include "graphics/IRenderer.h"
#include "graphics/dx10/CDevice10.h"
#include "graphics/animation/frame.h"

namespace xeres {

	/*!
		\class	CRenderer10
		\brief
	*/
	class CRenderer10 : public IRenderer
	{
	public:

		/// \ctor
		CRenderer10( void );

		/// \dtor
		virtual ~CRenderer10( void );

		DECL_IOBJECT_CLASS_DYNAMIC( CRenderer10 , IRenderer );

	public:

		/// \impl
		virtual void Initialize( RefWeak<IDevice> device , RefWeak<IData> config );

		/// \impl
		virtual void Finalize( void );

		/// \impl
		virtual ColorOrder GetColorOrder( void ) const;

		/// \impl
		virtual RefWeak<IDevice> GetDevice( void );

		/// \impl
		virtual void AddDeviceResource( RefWeak<IDeviceResource> res );

		/// \impl
		virtual void FreeDeviceResource( RefWeak<IDeviceResource> resource );

		/// \impl
		virtual void Tick( const Frame& frame );

		/// \impl
		virtual bool PreChangeMode( RefWeak<IDevice> device , void * oldSettings , void * newSettings );

		/// \impl
		virtual bool PostChangeMode( RefWeak<IDevice> device , void * settings );
		
		/// \impl
		virtual bool OnLostDevice( RefWeak<IDevice> device );

		/// \impl
		virtual bool OnResetDevice( RefWeak<IDevice> device );

		/// \impl
		virtual bool OnResizingSwapchain( RefWeak<IDevice> device );

		/// \impl
		virtual bool OnResizedSwapchain( RefWeak<IDevice> device );

		/// \impl
		virtual bool OnDestroyDevice( RefWeak<IDevice> device );

		/// \impl
		virtual RefPass<IPass> DefaultShadingPass( void );
		
		/// \impl
		virtual RefPass<IVertexLayout> DeclareVertexLayout( const std::vector<VertexSlotDeclaration>& decl );

		/// \impl
		virtual RefPass<IVertexBuffer> CreateDynamicVertex( RefWeak<ISlotLayout> layout , size_t vertex_count , const void * init_data , size_t size );

		/// \impl
		virtual RefPass<IVertexBuffer> CreateConstantVertex( RefWeak<ISlotLayout> layout , size_t vertex_count , const void * init_data , size_t size );

		/// \impl
		virtual RefPass<IVertexIndex> CreateConstantIndex( size_t count , size_t size , const void * init_data );

		/// \impl
		virtual RefPass<IVertexIndex> CreateDynamicIndex( size_t count , size_t size , const void * init_data );

		/// \impl
		virtual RefPass<IGeometry> CreateSimplePolygon(
			TopologyType type ,
			RefWeak<IVertexLayout> layout ,
			size_t vertex_count ,
			const void * vertex_data ,
			size_t index_count ,
			const ushort * index_data
			);

		/// \impl
		virtual RefPass<IGeometry> CreateGeometry(
			TopologyType type ,
			std::vector< RefWeak<IVertexBuffer> >& vertices );

		/// \impl
		virtual RefPass<IFont> CreateFont( const WString& faceName ,
			int height , int width , int weight , bool italic , bool highQuality , bool scalable );

		/// \impl
		virtual RefPass<ISprite> CreateSprite( void );

		/// \impl
		virtual RefPass<ITexture> CreateTextureForSprite( int width , int height );

		/// \impl
		virtual RefWeak<IRenderTarget> GetDefaultRenderTarget( void );

		/// \impl
		virtual RefWeak<IRenderTarget> GetDefaultDepthStencil( void );

		/// \impl
		virtual void RestoreDefaultRenderTarget( void );

		/// \impl
		virtual void RestoreDefaultDepthStencil( void );

		/// \impl
		virtual RefPass<IRenderTarget> CreateColorRenderTarget( int width , int height );

		/// \impl
		virtual RefPass<IRenderTargetSet> CreateRenderTargetSet( int width , int height );

		/// \impl
		virtual void SetEffectDefinition( const AString& name , const AString& def );

		/// \impl
		virtual void AddInclude( RefWeak<IFileSystem> filesystem , const WString& path );

		/// \impl
		virtual void DelInclude( RefWeak<IFileSystem> filesystem , const WString& path );

		/// \impl
		virtual RefPass<IEffect> CreateEffect( const void * buffer , size_t len , RefWeak<IEffectPool> pool );

		/// \impl
		virtual RefPass<IEffectPool> CreateEffectPool( const char * buffer , size_t len );

		/// \impl
		virtual RefWeak<IEffectPool> GetDefaultEffectPool( void );

		/// \impl
		virtual RefPass<IEffectCompiled> CompileEffect( const char * src , size_t len , RefWeak<IEffectPool> pool );

		/// \impl
		virtual RefPass<IShaderAdapter> LinkEffect( RefWeak<ITechnique> technique , RefWeak<IVertexLayout> layout );

		/// \impl
		virtual bool SetTexture( int shader_type , int sampler , RefWeak<ITexture> texture );

		/// \impl
		virtual RefPass<IRenderState> CreateStateForAll( void );

		/// \impl
		virtual RefPass<IBlendState> CreateBlendState( BlendSet& blend );

		/// \impl
		virtual void RestoreDefaultState( void );

	public:

		/// \brief	Set default pool.
		void SetDefaultPool( const AString& shader );

		// include path structure
		struct _IncludeDir
		{
			Ref<IFileSystem>		m_fs;
			WString					m_path;
		};

	private:

		// device
		Ref< CDevice10 >				m_device;

		// effects
		Ref< IEffectPool >				m_defaultPool;
		
		// default render target
		Ref< IRenderTarget >			m_defaultRenderTarget;
		Ref< IRenderTarget >			m_defaultDepthStencil;

		// effect compiler option
		std::vector< _IncludeDir >		m_includes;
		dict< AString , AString >		m_defines;

		// declared vertex declarations
		dict< AString , Ref<IVertexLayout> >	m_cachedLayout;
	};

} // namespace xeres
